package com.example.javafx.bilibilidownload.util;

import com.example.javafx.bilibilidownload.pojo.BiliVideoPojo;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author zengbang
 * @create 2024-08-12 17:12
 **/
public class BiliUtil {

    public static String getUpname(List<BiliVideoPojo> bilibiliVideos) {
        if (null == bilibiliVideos || bilibiliVideos.isEmpty()) return "";
        return bilibiliVideos.stream().map(BiliVideoPojo::getAuthor)
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet().stream()
                .max(Map.Entry.comparingByValue())
                .map(Map.Entry::getKey)
                .get();
    }

    public static void makeVideoNameByCreateTime(BiliVideoPojo biliVideoPojo) {
        // 设置视频地址和音频地址
        String audioName =
                biliVideoPojo.getCreated() + "-" + BiliFileUtil.fileNameReplaceInvalidCharater(biliVideoPojo.getTitle()) + ".mp3";
        String videoName =
                biliVideoPojo.getCreated() + "-" + BiliFileUtil.fileNameReplaceInvalidCharater(biliVideoPojo.getTitle()) + ".mp4";
        biliVideoPojo.setAudioName(audioName);
        biliVideoPojo.setVideoName(videoName);

    }

    public static void makeVideoNameByNull(BiliVideoPojo biliVideoPojo) {
        // 设置视频地址和音频地址
        String audioName = BiliFileUtil.fileNameReplaceInvalidCharater(biliVideoPojo.getTitle()) + ".mp3";
        String videoName = BiliFileUtil.fileNameReplaceInvalidCharater(biliVideoPojo.getTitle()) + ".mp4";
        biliVideoPojo.setAudioName(audioName);
        biliVideoPojo.setVideoName(videoName);

    }

    public static void makeVideoNameByFavoriteTime(BiliVideoPojo biliVideoPojo) {
        // 设置视频地址和音频地址,区分普通视频和合集视频
        String audioName = "";
        String videoName = "";
        // 普通视频有时间
        if (StringUtils.isEmpty(biliVideoPojo.getCollectionName())) {
            audioName =
                    biliVideoPojo.getFavoriteTime() + "-" + BiliFileUtil.fileNameReplaceInvalidCharater(biliVideoPojo.getTitle()) + ".mp3";
            videoName =
                    biliVideoPojo.getFavoriteTime() + "-" + BiliFileUtil.fileNameReplaceInvalidCharater(biliVideoPojo.getTitle()) + ".mp4";
        } else {
            // 合集视频不需要时间
            audioName = BiliFileUtil.fileNameReplaceInvalidCharater(biliVideoPojo.getTitle()) + ".mp3";
            videoName = BiliFileUtil.fileNameReplaceInvalidCharater(biliVideoPojo.getTitle()) + ".mp4";
        }
        biliVideoPojo.setAudioName(audioName);
        biliVideoPojo.setVideoName(videoName);
    }
}
